package com.ib.review.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ib.review.mapper.AnalyzeRecordMapper;
import com.ib.review.mapper.AnalyzeResultMapper;
import com.ib.review.pojo.dto.AnalyzeRecordQuery;
import com.ib.review.pojo.dto.AnalyzeResultParam;
import com.ib.review.pojo.dto.AnalyzeResultQuery;
import com.ib.review.pojo.po.AnalyzeRecordPO;
import com.ib.review.pojo.po.AnalyzeResultPO;
import com.ib.review.repository.ParsingRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public class ParsingRepositoryImpl implements ParsingRepository {
    @Resource
    private AnalyzeRecordMapper analyzeRecordMapper;
    @Resource
    private AnalyzeResultMapper analyzeResultMapper;

    public void insertRecord(AnalyzeRecordPO po) {
        analyzeRecordMapper.insert(po);
    }

    public List<AnalyzeRecordPO> selectByTimeRange(LocalDateTime localDateTime,String fileName,String userName) {
        QueryWrapper<AnalyzeRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", localDateTime);
        queryWrapper.eq("creator", userName);
        queryWrapper.eq("del_flag", 0);
        // 模糊查询条件
        if (fileName != null && !fileName.isEmpty()) {
            queryWrapper.like("file_name", fileName);
        }
        // 按创建时间降序排列（最新的在前）
        queryWrapper.orderByDesc("create_time");
        return analyzeRecordMapper.selectList(queryWrapper);
    }

    @Transactional
    public void deleteRecord(String recordId) {
        analyzeRecordMapper.updateByDelFlag(recordId);
        analyzeResultMapper.updateByDelFlag(recordId);
    }

    public List<AnalyzeResultPO> selectByRecordId(AnalyzeResultQuery analyzeResultQuery) {
        QueryWrapper<AnalyzeResultPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", analyzeResultQuery.getRecordId());
        queryWrapper.eq("del_flag", 0);
        List<Integer> types = analyzeResultQuery.getDataTypes();
        if(types != null && !types.isEmpty()) {
            queryWrapper.in("data_type",types);
        }
        queryWrapper.orderByAsc("page_num");
        return analyzeResultMapper.selectList(queryWrapper);
    }

    public void updateResult(AnalyzeResultParam analyzeResultParam) {
        analyzeResultMapper.updateResult(analyzeResultParam);
    }

    public AnalyzeRecordPO queryRecordById(AnalyzeRecordQuery analyzeRecordQuery) {
        QueryWrapper<AnalyzeRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", analyzeRecordQuery.getRecordId());
        queryWrapper.eq("del_flag", 0);
        return analyzeRecordMapper.selectOne(queryWrapper);
    }

    public Long queryResultCount(String recordId) {
        QueryWrapper<AnalyzeResultPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", recordId);
        queryWrapper.eq("del_flag", 0);
        return analyzeResultMapper.selectCount(queryWrapper);
    }

    public Long queryRecordCount(String fileName, String admin) {
        QueryWrapper<AnalyzeRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creator", admin);
        queryWrapper.eq("file_name", fileName);
        queryWrapper.eq("del_flag", 0);
        LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(2).withNano(0);
        queryWrapper.ge("create_time", threeDaysAgo);
        return analyzeRecordMapper.selectCount(queryWrapper);
    }

    public List<String> exportToExcel(String recordId) {
        return analyzeResultMapper.exportToExcel(recordId);
    }
}
